﻿using System.Drawing;
using System.Drawing.Drawing2D;

namespace MathService.Functions.Plotter.GraphComponents
{
    class Gridline : IGraphElement
    {
        private readonly Graph parentGraph;

        public Gridline(Graph parentGraph) { this.parentGraph = parentGraph; }

        #region IGraphElement Members

        public void Draw(Graphics graphics)
        {
            var pen = new Pen(parentGraph.GridlineColor)
                          {
                                      DashStyle = DashStyle.Dash
                          };
            Brush brush = new SolidBrush(parentGraph.GraphAreaColor);
            graphics.FillRectangle(brush, parentGraph.GraphArea);
            graphics.DrawRectangle(pen, parentGraph.GraphArea);
            if((parentGraph.Gridlines & GridStyles.Horizontal) == GridStyles.Horizontal)
            {
                graphics.SetClip(parentGraph.GraphArea);
                var num = parentGraph.GraphArea.Height/parentGraph.GraduationsY;
                for(var i = 0; i < parentGraph.GraphArea.Height; i += num)
                {
                    graphics.DrawLine(pen, parentGraph.GraphArea.Left, parentGraph.GraphArea.Top + i,
                                      parentGraph.GraphArea.Right, parentGraph.GraphArea.Top + i);
                }
            }
            if((parentGraph.Gridlines & GridStyles.Vertical) == GridStyles.Vertical)
            {
                var num3 = parentGraph.GraphArea.Width/parentGraph.GraduationsX;
                for(var j = 0; j < parentGraph.GraphArea.Width; j += num3)
                {
                    graphics.DrawLine(pen, parentGraph.GraphArea.Left + j, parentGraph.GraphArea.Bottom,
                                      parentGraph.GraphArea.Left + j, parentGraph.GraphArea.Top);
                }
            }
            pen.Dispose();
            brush.Dispose();
        }

        #endregion
    }
}
